<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="状态" prop="petStatus" style="width: 200px">
        <el-select v-model="queryParams.petStatus" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in pet_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名字" prop="petName">
        <el-input
          v-model="queryParams.petName"
          placeholder="请输入名字"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别 " prop="petSex" style="width: 200px">
        <el-select v-model="queryParams.petSex" placeholder="请选择性别 " clearable>
          <el-option
            v-for="dict in pet_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年龄" prop="petAge">
        <el-input
          v-model="queryParams.petAge"
          placeholder="请输入年龄"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品种" prop="petKind" style="width: 200px">
        <el-select v-model="queryParams.petKind" placeholder="请选择品种" clearable>
            <el-option
                    v-for="key in  kindList.value"
                    :key="key"
                    :label="key.petKind"
                    :value="key.petKindId"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="宠物大小" prop="petSize" style="width: 220px" >
        <el-select v-model="queryParams.petSize" placeholder="请选择宠物大小" clearable>
          <el-option
            v-for="dict in pet_size"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否绝育" prop="petNoBaby" style="width: 200px">
        <el-select v-model="queryParams.petNoBaby" placeholder="请选择是否绝育" clearable>
          <el-option
            v-for="dict in my_if"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否展示" prop="petShow" style="width: 220px">
        <el-select v-model="queryParams.petShow" placeholder="请选择是否展示" clearable>
          <el-option
            v-for="dict in my_if"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['pet:pets:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['pet:pets:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['pet:pets:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['pet:pets:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="petsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="petId" />
      <el-table-column label="状态" align="center" prop="petStatus">
        <template #default="scope">
          <dict-tag :options="pet_status" :value="scope.row.petStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="petImages" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.petImages" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="名字" align="center" prop="petName" />
      <el-table-column label="性别 " align="center" prop="petSex">
        <template #default="scope">
          <dict-tag :options="pet_sex" :value="scope.row.petSex"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="petAge" />
      <el-table-column label="品种" align="center" prop="petKind">
        <template #default="scope">
          <dict-tag :options="pet_kind" :value="scope.row.petKind"/>
        </template>
      </el-table-column>
      <el-table-column label="宠物性格" align="center" prop="petCharacter" />
      <el-table-column label="宠物大小" align="center" prop="petSize">
        <template #default="scope">
          <dict-tag :options="pet_size" :value="scope.row.petSize"/>
        </template>
      </el-table-column>
      <el-table-column label="收容故事或描述" align="center" prop="petText" width="700px" />
      <el-table-column label="健康情况" align="center" prop="petHealth" />
      <el-table-column label="是否绝育" align="center" prop="petNoBaby">
        <template #default="scope">
          <dict-tag :options="my_if" :value="scope.row.petNoBaby"/>
        </template>
      </el-table-column>
      <el-table-column label="已接种疫苗" align="center" prop="petVaccine" width="200"/>
      <el-table-column label="收容价格" align="center" prop="petPrice" />
      <el-table-column label="宠物领养价格" align="center" prop="petPeice" width="110"/>
      <el-table-column label="是否展示" align="center" prop="petShow" width="79">
        <template #default="scope">
          <dict-tag :options="my_if" :value="scope.row.petShow"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="petCreateAt" width="100">
        <template #default="scope">
          <span>{{ parseTime(scope.row.petCreateAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="petUpdateAt" width="100">
        <template #default="scope">
          <span>{{ parseTime(scope.row.petUpdateAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['pet:pets:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['pet:pets:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改宠物信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="petsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="状态" prop="petStatus">
          <el-select v-model="form.petStatus" placeholder="请选择状态">
            <el-option
              v-for="dict in pet_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="petImages">
          <image-upload v-model="form.petImages"/>
        </el-form-item>
        <el-form-item label="名字" prop="petName">
          <el-input v-model="form.petName" placeholder="请输入名字" />
        </el-form-item>
        <el-form-item label="性别 " prop="petSex">
          <el-select v-model="form.petSex" placeholder="请选择性别 ">
            <el-option
              v-for="dict in pet_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="petAge">
          <el-input v-model="form.petAge" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="品种" prop="petKind" clearable >
          <el-select v-model="form.petKind" placeholder="请选择品种">
          <el-option
                  v-for="key in  kindList.value"
                  :key="key"
                  :label="key.petKind"
                  :value="key.petKindId"></el-option>
        </el-select>

        </el-form-item>
        <el-form-item label="宠物性格" prop="petCharacter">
          <el-input v-model="form.petCharacter" placeholder="请输入宠物性格" />
        </el-form-item>
        <el-form-item label="宠物大小" prop="petSize">
          <el-select v-model="form.petSize" placeholder="请选择宠物大小">
            <el-option
              v-for="dict in pet_size"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收容故事或描述" prop="petText">
          <el-input v-model="form.petText" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="健康情况" prop="petHealth">
          <el-input v-model="form.petHealth" placeholder="请输入健康情况" />
        </el-form-item>
        <el-form-item label="是否绝育" prop="petNoBaby">
          <el-select v-model="form.petNoBaby" placeholder="请选择是否绝育">
            <el-option
              v-for="dict in my_if"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="已接种疫苗" prop="petVaccine">
          <el-input v-model="form.petVaccine" placeholder="请输入已接种疫苗" />
        </el-form-item>
        <el-form-item label="收容价格" prop="petPrice">
          <el-input v-model="form.petPrice" placeholder="请输入收容价格" />
        </el-form-item>
        <el-form-item label="宠物领养价格" prop="petPeice">
          <el-input v-model="form.petPeice" placeholder="请输入宠物领养价格" />
        </el-form-item>
        <el-form-item label="是否展示" prop="petShow">
          <el-select v-model="form.petShow" placeholder="请选择是否展示">
            <el-option
              v-for="dict in my_if"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Pets">
import { listPets, getPets, delPets, addPets, updatePets } from "@/api/pet/pets";
import {reactive} from "@vue/reactivity";

const { proxy } = getCurrentInstance();
const { pet_sex, pet_size, pet_kind, my_if, pet_status } = proxy.useDict('pet_sex', 'pet_size', 'pet_kind', 'my_if', 'pet_status');

const petsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    petStatus: null,
    petName: null,
    petSex: null,
    petAge: null,
    petKind: null,
    petSize: null,
    petNoBaby: null,
    petShow: null,
  },
  rules: {
    petSex: [
      { required: true, message: "性别 不能为空", trigger: "change" }
    ],
    petAge: [
      { required: true, message: "年龄不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询宠物信息列表 */
function getList() {
  loading.value = true;
  listPets(queryParams.value).then(response => {
    petsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    petId: null,
    petStatus: null,
    petImages: null,
    petName: null,
    petSex: null,
    petAge: null,
    petKind: null,
    petCharacter: null,
    petSize: null,
    petText: null,
    petHealth: null,
    petNoBaby: null,
    petVaccine: null,
    petPrice: null,
    petPeice: null,
    petShow: null,
    petCreateAt: null,
    petUpdateAt: null
  };
  proxy.resetForm("petsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.petId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加宠物信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _petId = row.petId || ids.value
  getPets(_petId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改宠物信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["petsRef"].validate(valid => {
    if (valid) {
      if (form.value.petId != null) {
        updatePets(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPets(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _petIds = row.petId || ids.value;
  proxy.$modal.confirm('是否确认删除宠物信息编号为"' + _petIds + '"的数据项？').then(function() {
    return delPets(_petIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('pet/pets/export', {
    ...queryParams.value
  }, `pets_${new Date().getTime()}.xlsx`)
}

import {getKindList} from "@/api/pet/kind.js";
//获取当前宠物的类型
const kindList = reactive([])
getKindList().then((res)=>{
  kindList.value = res.rows
  console.log(res)
});
getKindList()
getList();
</script>
